﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using App_Code.SiteParsers;

namespace App_Code.Helpers
{
    /// <summary>
    /// Summary description for PluginHandler
    /// </summary>
    public class PluginHandler
    {
        private static IEnumerable<ISiteScanner> InitializePlugins()
        {
            return new List<ISiteScanner>
                           {
                               //new TopDownMeParser(),
                               //new SdarotOrg(),
                               new MyVodParser(), 
                               //new VipmovieNetParser(), 
                               //new SratimoNet(),
                               //new TenNetILParser(),
                               //new SdarotNet(),
                               //new CuevanaTV()
                           };
        }

        public static void DoWork(int moviesPagesToScan, int seriesPagesToScan)
        {
            var plugins = InitializePlugins();
            var lastMoviesPageNumber = moviesPagesToScan;
            var lastSeriesPageNumber = seriesPagesToScan;
            var pluginDBHandler = PluginDBHandler.Instance;

            foreach (var scanner in plugins)
            {
                try
                {
                    Debug.WriteLine(string.Format("Start scanning movies for site: {0} - {1}", DateTime.Now, scanner.Name));

                    scanner.DlgAddVideoHandler = pluginDBHandler.AddNewVideo;
                    scanner.DlgIsVideoExistsHandler = pluginDBHandler.IsVideoExists;

                    if (moviesPagesToScan == 0)
                        lastMoviesPageNumber = scanner.GetMoviesLastPageNumber();
                    scanner.ScanForMovies(lastMoviesPageNumber);

                    if (seriesPagesToScan == 0)
                        lastSeriesPageNumber = scanner.GetSeriesLastPageNumber();
                    scanner.ScanForSeries(lastSeriesPageNumber);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Exception in DoWork when scanning: {0} At {1} Error:{2}", scanner.Name, DateTime.Now, ex.Message));
                }
            }

            Debug.WriteLine(string.Format("DoWork finish its work at {0}", DateTime.Now));

        }
    }
}